package com.qidianit.mes.production.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.qidianit.common.pojo.MESResult;
import com.qidianit.mes.pojo.ProcessSteps;
import com.qidianit.mes.production.service.ProcessStepService;
import org.apache.ibatis.annotations.Param;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.web.bind.annotation.*;

import java.io.FileNotFoundException;
import java.util.List;

/**
 * @Author djl
 * @Time 2024/10/17 9:53
 */
@RestController
@RequestMapping("/processStep")
@CrossOrigin
public class ProcessStepController {
    @Autowired
    private ProcessStepService processStepService;
    @GetMapping("/list")
    public MESResult list(String processId,
                          @RequestParam(defaultValue = "1")int page,
                          @RequestParam(defaultValue = "10")int limit){
        return processStepService.showProcessStep(processId,page,limit);
    }
    @GetMapping("/count")
    public MESResult countProcessSteps(String processId){
        return processStepService.countProcessSteps(processId);
    }
    @RequestMapping("/add")
    public MESResult addNewStep(@RequestBody ProcessSteps processSteps){
        return  processStepService.addNewStep(processSteps);
    }
    @PostMapping("/edit")
    public MESResult editStep(@RequestBody ProcessSteps processSteps, @Param("beforeStepId") int beforeStepId){
        return processStepService.editStep(processSteps,beforeStepId);
    }
    @DeleteMapping("/delete")
    public MESResult deleteStep(String processId,int processStepId){
        return processStepService.deleteStep(processId,processStepId);
    }
    @DeleteMapping("/reDelete")
    public MESResult reDelete(int[] stepIds,String processId){
        return processStepService.reDelete(stepIds,processId);
    }
    @RequestMapping("/download")
    public MESResult download(String processId ,String name,String address){
        // 查询数据
        MESResult result = processStepService.showProcessStep(processId, 1, 100);
        PageInfo<ProcessSteps> pageInfo = (PageInfo<ProcessSteps>) result.getData();
        List<ProcessSteps> processSteps = pageInfo.getList();
        // 脱敏处理
//        processSteps.forEach(dict-> DesensitivityUtil.desensitization(dict));
        //  通过EasyExcel写数据
        int num = 200;
        try{
            EasyExcel.write(address + "\\" + name,ProcessSteps.class)
                    .sheet("步骤信息")
                    .doWrite(processSteps);
        }catch (Exception e){
            num = 300;
        }
        return MESResult.success(num,"OK");
    }
    @GetMapping("/showByOperationStepId")
    public MESResult showByOperationStepId(Integer operationStepId){
        return processStepService.showByOperationStepId(operationStepId);
    }
}
